﻿//#define INTERNAL_DEBUG

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Text;

using Artisteer_Project_Converter.Properties;
using Artisteer_Project_Converter.ArtisteerProjectApi;
using Artisteer_Project_Converter.VisualStudioApi;

namespace Artisteer_Project_Converter
{
    public class Program
    {
        [STAThread()]
        public static void Main(string[] args)
        {
            string targetDirectory = "";
            string projectName = "";

            AssemblyName assemblyInfo = Assembly.GetExecutingAssembly().GetName();

            Console.WriteLine(string.Format(Resources.Title_Application, assemblyInfo.Name, assemblyInfo.Version));
            Console.WriteLine(Resources.Title_Copyright);
            Console.WriteLine();
            Console.WriteLine(Resources.Title_Description);
            Console.WriteLine();
            Console.WriteLine();

            try
            {
                Console.WriteLine(Resources.Prompt_SelectSourceFolder);
                Console.WriteLine();

                ArtProject artProject = new ArtProject();

                targetDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
                projectName = Path.GetFileName(artProject.BasePath);

                convertProject(artProject, targetDirectory, projectName);

                Directory.Move(artProject.BasePath, artProject.BasePath + ".bak");

                for (int retryCounter = 0; retryCounter < 10; retryCounter++)
                {
                    try
                    {
                        Directory.Move(Path.Combine(targetDirectory, projectName), artProject.BasePath);
                        break;
                    }
                    catch
                    {
                        Thread.Sleep(1000);
                    }
                }

                if (!Directory.Exists(artProject.BasePath))
                    Directory.Move(Path.Combine(targetDirectory, projectName), artProject.BasePath);
            }
#if !INTERNAL_DEBUG
            catch (Exception ex)
            {
                Console.WriteLine();
                Console.WriteLine(Resources.Message_ConversionFailed);
                Console.Error.WriteLine(ex.Message);
                Console.Error.WriteLine(ex.StackTrace);

                Console.WriteLine();
                Console.WriteLine(Resources.Prompt_PressAnyKey);
                Console.ReadKey();
                return;
            }
#endif
            finally
            {
                if (targetDirectory != "" && Directory.Exists(targetDirectory))
                    Directory.Delete(targetDirectory, true);
            }
        }

        /// <summary>
        /// Convert Artisteer project
        /// </summary>
        /// <param name="artProject">Artisteer project</param>
        /// <param name="targetPath">Location to store converted project</param>
        /// <param name="projectName">Name of converted project</param>
        static private void convertProject(ArtProject artProject,
                                           string targetPath,
                                           string projectName)
        {
            string solutionPath = Path.Combine(targetPath, projectName);
            string solutionFile = Path.Combine(solutionPath, projectName + ".sln");
            string projectPath = Path.Combine(solutionPath, projectName);

            // Run Visual Studio
            Console.WriteLine(Resources.Message_StartingVisualStudio);

            VisualStudio vs = new VisualStudio();

#if !INTERNAL_DEBUG
            try
            {
#endif
                // Create a new solution folder
                Console.WriteLine(Resources.Message_CreatingSolutionFolder);
                VsSolution vsSolution = vs.CreateSolution(solutionPath, projectName);

                // Create a new Web Application Project
                VsProject vsProject = vsSolution.CreateProject("EmptyWebApplicationProject40.zip", "CSharp" , projectPath, projectName);

                vsProject.AddReference("System.Design.dll");

                // Convert Artisteer project
                artProject.Convert(vsProject, projectPath);

                // Apply template
                artProject.ApplyTemplate(vsProject,
                                         Path.Combine(Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]), "Template"),
                                         projectPath);

                // Adjust JavaScripts
                artProject.AdjustJavaScript(projectPath);

                // Save project file
                Console.WriteLine(Resources.Message_SavingProjectFile);
                vsProject.Save();

                // Save solution file
                Console.WriteLine(Resources.Message_SavingSolutionFile);
                vsSolution.SaveAs(solutionFile);
            
                // Success Message
                Console.WriteLine();
                Console.WriteLine(Resources.Message_ConversionSuccessful);
#if !INTERNAL_DEBUG
            }
            finally
            {
#endif
                // Quit Visual Studio
                Console.WriteLine(Resources.Message_ExitingVisualStudio);
                vs.Dispose();
#if !INTERNAL_DEBUG
            }
#endif
        }
    }
}
